package com.tl.commonlibrary.ui.picture.bean;

import android.net.Uri;
import android.text.TextUtils;

import com.luck.picture.lib.entity.LocalMedia;

import java.io.File;
import java.io.Serializable;

/**
 * Created by Administrator on 2017/6/13 0013.
 */

public class Picture implements Serializable {
    private LocalMedia localMedia;//专供Selector图片使用
    private String resourceId;//网络图片资源id
    private String path;//图片地址
    public Picture() {

    }

    /**
     * 网络图片地址存储
     * 自动转换成LocalMedia以及Uri
     *
     * @param path
     */
    public void setHttpPath(String path) {
        this.path = path;
        if (localMedia == null) {
            localMedia = new LocalMedia();
        }
        localMedia.setPath(path);
        localMedia.setCut(false);
        localMedia.setCompressed(false);
        setLocalMedia(localMedia);
    }

    public void setLocalMedia(LocalMedia localMedia) {
        this.localMedia = localMedia;
        this.path = getLocalMediaPath();
        String suffix = "";
        if (!TextUtils.isEmpty(path)) {
            //兼容localMedia.getPictureType()源码报空指针的问题
            int dotIndex = path.lastIndexOf(".");
            if (dotIndex >= 0) {
                suffix = path.substring(dotIndex + 1, path.length());
            }
        }
        this.localMedia.setPictureType(suffix);
    }

    public void setResourceId(String resourceId) {
        this.resourceId = resourceId;
    }


    public Uri getUri() {
        if (path==null){
            return  null;
        }else {
            if (path.startsWith("http")) {
                return Uri.parse(path);
            } else {
                return Uri.fromFile(new File(path));
            }
        }
    }

    /**
     * 专门提供给Selector图片查看器使用
     *
     * @return
     */
    public LocalMedia getLocalMedia() {
        return localMedia;
    }

    public String getLocalMediaPath() {
        String path = "";
        if (localMedia.isCut() && !localMedia.isCompressed()) {
            // 裁剪过
            path = localMedia.getCutPath();
        } else if (localMedia.isCompressed()) {
            // 压缩过,或者裁剪同时压缩过,以最终压缩过图片为准
            path = localMedia.getCompressPath();
        } else {
            // 原图
            path = localMedia.getPath();
        }
        return path;
    }

    public String getResourceId() {
        return resourceId;
    }


    public String getHttpPath() {
        return path;
    }




}
